package com.hongye.app.model;

import java.util.List;

import com.hongye.util.Utils;
import com.jfinal.plugin.activerecord.Model;

public class PaymentModel extends Model<PaymentModel>{

	private static final long serialVersionUID = 4886363967527294311L;

	public static final PaymentModel dao=new PaymentModel();

	/**
	 * 添加payment
	 * @author liushaomin
	 * 2016年12月6日
	 * @param orders
	 * @param channel 支付渠道
	 * @return
	 */
	public void addPayment(List<OrderInfoModel> orders, String channel) {
		for (OrderInfoModel item : orders) {
			String sql = "select * from payment where ordersn = '"+item.get("order_sn")+"' and state = 0 and type = 0 and channel = '"+channel+"'";
			PaymentModel pay = dao.findFirst(sql);
			if(pay == null){
				new PaymentModel().set("ordersn", item.get("order_sn")).set("orderid", item.get("id")).set("user_id", item.get("user_id"))
				.set("user_phone", item.get("phone")).set("money", item.get("total_price")).set("type", 0).set("channel", channel)
				.set("c_time", Utils.getDate()).set("state", 0).save();
			}
		}
	}
	
	/**
	 * 根据订单号获取payment
	 * @author liushaomin
	 * 2016年12月7日
	 * @param ordersn
	 * @return
	 */
	public PaymentModel getPaymentByOrdersn(String ordersn) {
		String sql = "select * from payment where ordersn = '"+ ordersn +"'";
		PaymentModel pay = dao.findFirst(sql);
		return pay;
	}
	
	/**
	 * 筛选交易信息列表
	 * @author lifuqiang
	 * 2016-12-20
	 * @param time_start
	 * @param time_end
	 * @param state
	 * @return
	 */
	public List<PaymentModel> getPaymentByScreen(String time_start, String time_end, String state) {
		String sql = "select *, c_time as c_time from payment where c_time >= '"+time_start+"' and c_time <= '"+time_end+"' and state in ( "+state+" ) order by c_time desc";
		List<PaymentModel> pay_list = dao.find(sql);
		return pay_list;
	}

	/**
	 * 根据订单号获取payment
	 * @author liushaomin
	 * 2017年2月21日
	 * @param ordersn 订单号
	 * @param channel 微信or支付宝
	 * @param state 当前状态
	 * @return
	 */
	public static PaymentModel getPaymentByOrderSN_Channel_state(String ordersn, String channel, int state) {
		 String sql = "select * from payment where ordersn = "+ordersn+" and type = 0 and state = '"+ state +"' and channel = '"+channel+"'";
		 return  dao.findFirst(sql);
	}


	/**
	 * 根据订单id的集合获取payment
	 * @author liushaomin
	 * 2017年2月21日
	 * @param orderids 订单id的集合
	 * @param channel 微信or支付宝
	 * @return
	 */
	public static List<PaymentModel> getListByOrderids_Channel(String orderids, String channel) {
		String  sql = "select * from payment where orderid in ("+ orderids +") and type = 0 and state = 0 and channel = '"+channel+"'";
		return dao.find(sql);
	}
}
